from selenium.common.exceptions import NoSuchElementException
import unittest, time, re, types


class PasswordChangeTest(object):
    def test_password_change(self):
        self.go_to_main_page()
        self.change_my_password('Ann', 'qwe', '123')
        self.change_password('admin', 'admin', 'Ann', '123', 'qwe')

    def login_as(self, login, passwd):
        self.driver.find_element_by_link_text("Log in").click()
        userNameField = self.driver.find_element_by_id("username")
        userNameField.clear()
        userNameField.send_keys(login)
        passwordField = self.driver.find_element_by_name("password")
        passwordField.clear()
        passwordField.send_keys(passwd)
        self.waiter.until(lambda drv: drv.find_element_by_name("submit")).click()
        assert self.driver.find_element_by_link_text("Log out")

    def go_to_main_page(self):
        self.driver.get(self.base_url + "/php4dvd/")

    def go_to_my_profile(self, user):
        self.driver.find_element_by_link_text("My profile").click()
        #self.driver.find_element_by_link_text("Home").click()
        #self.assert_(self.driver.find_element_by_xpath("//*/h2[text()[contains(.,'Edit %s')]]" % user), 'Not page "My profile"')
        assert self.driver.find_element_by_xpath("//*/h2[text()[contains(.,'Edit %s')]]" % user)

    def go_to_user_managment(self):
        self.driver.find_element_by_link_text("User management").click()
        assert self.driver.find_element_by_xpath("//*/h2[text()[contains(.,'User management')]]")

    def change_my_password(self, user, old_passwd, new_pass):
        self.login_as(user, old_passwd)
        self.go_to_my_profile(user)
        self.driver.find_element_by_id("password").clear()
        self.driver.find_element_by_id("password").send_keys(new_pass)
        self.driver.find_element_by_id("password2").clear()
        self.driver.find_element_by_id("password2").send_keys(new_pass)
        self.driver.find_element_by_name("submit").click()
        self.logout()

    def change_password(self, admin, admin_pass, user, old_passwd, new_pass):
        self.login_as(admin, admin_pass)
        self.go_to_user_managment()
        try:
            self.driver.find_element_by_xpath(u"//*/a[contains(text(),'%s')]" % user).click()
        except NoSuchElementException as e:
            return False
        self.driver.find_element_by_id("password").clear()
        self.driver.find_element_by_id("password").send_keys(new_pass)
        self.driver.find_element_by_id("password2").clear()
        self.driver.find_element_by_id("password2").send_keys(new_pass)
        self.driver.find_element_by_name("submit").click()
        self.logout()

    def logout(self):
        self.waiter.until(lambda drv: drv.find_element_by_link_text("Log out")).click()
#        self.driver.find_element_by_link_text("Log out").click()
        self.waiter.until(lambda drv: drv.switch_to_alert()).accept()
#        self.driver.switch_to_alert().accept()
        self.assert_(self.is_on_login_page(), "Page not found!")

    def is_on_login_page(self):
        return self.driver.find_element_by_link_text("Log in")

if __name__ == "__main__":
    unittest.main()

